home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 44 / Amiga Format CD44 (1999-08-26)(Future Publishing)(GB)(Track 1 of 3)[!][issue 1999-10].iso / -serious- / misc / calculator / window.c < prev    next >
C/C++ Source or Header  |  1999-07-12  |  13KB  |  338 lines

  1. /* Window.c */
  2.  
  3. /* This file contains the main execution loop and all code for handling
  4. program functions */
  5.  
  6. #include <exec/types.h>
  7. #include <clib/exec_protos.h>
  8. #include <intuition/intuition.h>
  9. #include <intuition/IntuitionBase.h>
  10. #include <intuition/gadgetclass.h>
  11. #include <libraries/gadtools.h>
  12. #include <graphics/gfxmacros.h>
  13. #include <graphics/GfxBase.h>
  14. #include <clib/dos_protos.h>
  15. #include <clib/intuition_protos.h>
  16. #include <clib/gadtools_protos.h>
  17. #include <clib/graphics_protos.h>
  18. #include <string.h>
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21.  
  22. #include "Gadgets.c"
  23.  
  24. /*
  25. ** Function for defining and displaying About Alert message.
  26. */
  27. void about_alert()
  28. {
  29.     char message[156];
  30.  
  31.     strcpy(message, "   Calculator program created by Donald W Millican.");
  32.     strcat(message,
  33.           "     (C) Copyright 1999 DWM Productions.");
  34.     strcat(message,
  35.           "     Version 1.2 16/06/99. Press Left Mouse Button to continue.");
  36.  
  37.     message[0]=0;       /* X position of the first string */
  38.     message[1]=32;      /*               - " -            */
  39.     message[2]=16;      /* Y             - " -            */
  40.  
  41.     message[51]='\0';   /* NULL sign which finish of the first string. */
  42.     message[52]=TRUE;   /* Continuation byte set to TRUE (new string). */
  43.  
  44.     message[53]=0;
  45.     message[54]=32;
  46.     message[55]=32;
  47.  
  48.     message[91]='\0';
  49.     message[92]=TRUE;
  50.  
  51.     message[93]=0;      /* X position of the third string. */
  52.     message[94]=32;     /*               - " -             */
  53.     message[95]=48;     /* Y             - " -             */
  54.  
  55.     message[154]='\0';  /* NULL sign which finish of the third string.   */
  56.     message[155]=FALSE; /* Continuation byte set to FALSE (last string). */
  57.  
  58.     /* We will now display the Alert message: */
  59.     DisplayAlert( RECOVERY_ALERT, message, 64 );
  60. }
  61.  
  62. /*
  63. ** Standard message handling loop with GadTools message handling functions
  64. ** used (GT_GetIMsg() and GT_ReplyIMsg()).
  65. */
  66. void process_window_events(struct Screen *mysc, struct Window *mywin,
  67.     struct Gadget *my_gads[], struct Menu *mainmenustrip, struct StringInfo *gad_ptr[])
  68. {
  69.     struct IntuiMessage *imsg;
  70.     ULONG imsgClass;
  71.     UWORD imsgCode;
  72.     BOOL terminated = FALSE;
  73.     struct Gadget *gad;
  74.     UWORD menu_number, which_menu, which_item;
  75.     MenuItem *item;
  76.     int total = 0;
  77.     int entered_number = 0, no_figures = 0, sum = 0;
  78.     BOOL special_button = FALSE;
  79.     BOOL add = FALSE, subtract = FALSE, multiply = FALSE, divide = FALSE;
  80.  
  81. while (!terminated)
  82.     {
  83.     Wait (1 << mywin->UserPort->mp_SigBit);
  84.  
  85.     /* GT_GetIMsg() returns an IntuiMessage with more friendly information for
  86.     ** complex gadget classes.  Use it wherever you get IntuiMessages where
  87.     ** using GadTools gadgets.
  88.     */
  89.  
  90.     while ((!terminated) &&
  91.            (imsg = GT_GetIMsg(mywin->UserPort)))
  92.         {
  93.         /* Presuming a gadget, of course, but no harm...
  94.         ** Only dereference this value (gad) where the Class specifies
  95.         ** that it is a gadget event.
  96.         */
  97.         gad = (struct Gadget *)imsg->IAddress;
  98.  
  99.         imsgClass = imsg->Class;
  100.         imsgCode = imsg->Code;
  101.         menu_number = imsg->Code;
  102.  
  103.         /* Use the toolkit message-replying function here... */
  104.         GT_ReplyIMsg(imsg);
  105.  
  106.         switch (imsgClass)
  107.             {
  108.             /*  --- WARNING --- WARNING --- WARNING --- WARNING --- WARNING ---
  109.             ** GadTools puts the gadget address into IAddress of IDCMP_MOUSEMOVE
  110.             ** messages.  This is NOT true for standard Intuition messages,
  111.             ** but is an added feature of GadTools.
  112.             */
  113.             case IDCMP_VANILLAKEY:
  114.             case IDCMP_GADGETUP:
  115.  
  116.                 special_button = handleGadgetEvent(mysc, mywin, gad, imsgCode, my_gads, entered_number, no_figures);
  117.  
  118.                 if(special_button == FALSE)
  119.                 {
  120.                 if(no_figures == 0)
  121.                 {
  122.                     total = entered_number;
  123.                     GT_SetGadgetAttrs(my_gads[MYGAD_DISPLAY], mywin, NULL, GTNM_Number, total, TAG_END);
  124.                 }
  125.                 if(no_figures >= 1 && no_figures <= 10)
  126.                 {
  127.                     if(entered_number == 10)
  128.                     {
  129.                         total *= 10;
  130.                     } else
  131.                     {
  132.                         total *= 10;
  133.                         total += entered_number;
  134.                     }
  135.                         GT_SetGadgetAttrs(my_gads[MYGAD_DISPLAY], mywin, NULL, GTNM_Number, total, TAG_END);
  136.                 }
  137.  
  138.                 } else // if special button == TRUE
  139.                 {
  140.                     if(gad->GadgetID == MYGAD_CLEAR)
  141.                     {
  142.                         sum = 0;
  143.                         total = 0;
  144.                         no_figures = 0;
  145.                         add = subtract = multiply = divide = FALSE;
  146.                         GT_SetGadgetAttrs(my_gads[MYGAD_DISPLAY], mywin, NULL, GTNM_Number, 0, TAG_END);
  147.                     }
  148.                     if(gad->GadgetID == MYGAD_ADD || imsgCode == '+')
  149.                     {
  150.                         if(no_figures > 0 && no_figures < 10)
  151.                         {
  152.                             if(subtract == TRUE)
  153.                             {
  154.                                 sum -= total;
  155.                                 subtract = FALSE;
  156.                                 add = TRUE;
  157.                             } else
  158.                             if(add == TRUE)
  159.                             {
  160.                                 sum += total;
  161.                                 add = TRUE;
  162.                                 subtract = FALSE;
  163.                             } else
  164.                             {
  165.                                 sum += total;
  166.                                 add = TRUE;
  167.                                 subtract = FALSE;
  168.                             }
  169.                             total = 0;
  170.                             no_figures = 0;
  171.                             entered_number = 0;
  172.                             GT_SetGadgetAttrs(my_gads[MYGAD_DISPLAY], mywin, NULL, GTNM_Number, sum, TAG_END);
  173.                         }
  174.                     }
  175.                     if(gad->GadgetID == MYGAD_SUBTRACT || imsgCode == '-')
  176.                     {
  177.                         if(no_figures > 0 && no_figures < 10)
  178.                         {
  179.                             if(add == TRUE)
  180.                             {
  181.                                 sum += total;
  182.                                 add = FALSE;
  183.                                 subtract = TRUE;
  184.                             } else
  185.                             if(subtract == TRUE)
  186.                             {
  187.                                 sum -= total;
  188.                                 subtract = TRUE;
  189.                                 add = FALSE;
  190.                             } else
  191.                             {
  192.                                 sum += total;
  193.                                 subtract = TRUE;
  194.                                 add = FALSE;
  195.                             }
  196.                             total = 0;
  197.                             no_figures = 0;
  198.                             entered_number = 0;
  199.                             GT_SetGadgetAttrs(my_gads[MYGAD_DISPLAY], mywin, NULL, GTNM_Number, sum, TAG_END);
  200.                         }
  201.                     }
  202.                     if(gad->GadgetID == MYGAD_MULTIPLY || imsgCode == '*')
  203.                     {
  204.                         if(no_figures > 0 && no_figures < 10)
  205.                         {
  206.                             if(divide == TRUE)
  207.                             {
  208.                                 sum /= total;
  209.                                 divide = FALSE;
  210.                                 multiply = TRUE;
  211.                             } else
  212.                             if(multiply == TRUE)
  213.                             {
  214.                                 sum *= total;
  215.                                 multiply = TRUE;
  216.                                 divide = FALSE;
  217.                             } else
  218.                             {
  219.                                 sum += total;
  220.                                 multiply = TRUE;
  221.                                 divide = FALSE;
  222.                             }
  223.                             total = 0;
  224.                             no_figures = 0;
  225.                             entered_number = 0;
  226.                             GT_SetGadgetAttrs(my_gads[MYGAD_DISPLAY], mywin, NULL, GTNM_Number, sum, TAG_END);
  227.                         }
  228.                     }
  229.                     if(gad->GadgetID == MYGAD_DIVIDE || imsgCode == '/')
  230.                     {
  231.                         if(no_figures > 0 && no_figures < 10)
  232.                         {
  233.                             if(multiply == TRUE)
  234.                             {
  235.                                 sum *= total;
  236.                                 multiply = FALSE;
  237.                                 divide = TRUE;
  238.                             } else
  239.                             if(divide == TRUE)
  240.                             {
  241.                                 sum /= total;
  242.                                 divide = TRUE;
  243.                                 multiply = FALSE;
  244.                             } else
  245.                             {
  246.                                 sum += total;
  247.                                 divide = TRUE;
  248.                                 multiply = FALSE;
  249.                             }
  250.                             total = 0;
  251.                             no_figures = 0;
  252.                             entered_number = 0;
  253.                             GT_SetGadgetAttrs(my_gads[MYGAD_DISPLAY], mywin, NULL, GTNM_Number, sum, TAG_END);
  254.                         }
  255.                     }
  256.                     if(gad->GadgetID == MYGAD_CHANGE_SIGN)
  257.                     {
  258.                         if(total > 0)
  259.                         {
  260.                             total = total - (total * 2);
  261.                         } else if(total < 0)
  262.                         {
  263.                             total = total - (total * 2);
  264.                         }
  265.                         GT_SetGadgetAttrs(my_gads[MYGAD_DISPLAY], mywin, NULL, GTNM_Number, total, TAG_END);
  266.                     }
  267.                     if(gad->GadgetID == MYGAD_EQUALS)
  268.                     {
  269.                         if(no_figures < 11)
  270.                         {
  271.                             if(add == TRUE)
  272.                                 sum += total;
  273.                             if(subtract == TRUE)
  274.                                 sum -= total;
  275.                             if(multiply == TRUE)
  276.                                 sum *= total;
  277.                             if(divide == TRUE)
  278.                                 sum /= total;
  279.                             if(sum == 0)
  280.                                 GT_SetGadgetAttrs(my_gads[MYGAD_DISPLAY], mywin, NULL, GTNM_Number, total, TAG_END);
  281.                             else
  282.                                 GT_SetGadgetAttrs(my_gads[MYGAD_DISPLAY], mywin, NULL, GTNM_Number, sum, TAG_END);
  283.                         }
  284.                     }
  285.                     special_button = FALSE;
  286.                 }
  287.  
  288.                     // Redraw gadgets to show updates
  289.                     GT_BeginRefresh(mywin);
  290.                     GT_EndRefresh(mywin, TRUE);
  291.  
  292.                     // reset gadget pointer
  293.                     gad = NULL;
  294.  
  295.                 break;
  296.             case IDCMP_MENUPICK:
  297.                 // Menu chosen
  298.                 which_menu=MENUNUM(menu_number);
  299.                 which_item=ITEMNUM(menu_number);
  300.  
  301.                 while( menu_number != MENUNULL )
  302.                 {
  303.                     /* Get the address of the item: */
  304.                     item = (struct MenuItem *) ItemAddress( mainmenustrip, menu_number );
  305.  
  306.                     /* Check which item was selected: */
  307.                     if( which_menu == 0 )
  308.                     {
  309.                         if( which_item == 0 )
  310.                         {
  311.                             // about selected
  312.                             about_alert();
  313.                         }
  314.                         if( which_item == 1 )
  315.                         {
  316.                             terminated = TRUE;
  317.                         }
  318.                     }
  319.                     /* Get the following item's menu number: */
  320.                     menu_number = item->NextSelect;
  321.                 }
  322.                 break;
  323.             case IDCMP_CLOSEWINDOW:
  324.                 terminated = TRUE;
  325.                 break;
  326.             case IDCMP_REFRESHWINDOW:
  327.                 /* With GadTools, the application must use GT_BeginRefresh()
  328.                 ** where it would normally have used BeginRefresh()
  329.                 */
  330.                 GT_BeginRefresh(mywin);
  331.                 GT_EndRefresh(mywin, TRUE);
  332.                 break;
  333.             }
  334.         }
  335.     }
  336. }
  337.  
  338.